/**
 * Title: PhoneNumberListAdapter.java
 * Description:
 * Copyright: Copyright (c) 2008
 * Company:深圳彩讯科技有限公司
 * Author 罗旭东 (hi@luoxudong.com)
 * Date 2014-1-13 上午10:32:18
 * Version 1.0
 */
package com.luoxudong.phonenumber.ui.adapters;

import java.util.List;

import android.content.Context;
import android.text.Html;
import android.text.Spanned;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.luoxudong.phonenumber.R;
import com.luoxudong.phonenumber.logic.area.AreaFactory;
import com.luoxudong.phonenumber.logic.area.interfaces.IAreaManager;
import com.luoxudong.phonenumber.logic.common.value.TelOperatorType;
import com.luoxudong.phonenumber.logic.db.model.AreaInfo;
import com.luoxudong.phonenumber.logic.db.model.PhoneNumber;

/** 
 * ClassName: PhoneNumberListAdapter
 * Description:TODO(这里用一句话描述这个类的作用)
 * Create by 罗旭东
 * Date 2014-1-13 上午10:32:18
 */
public class PhoneNumberListAdapter extends AbsBaseAdapter<PhoneNumber>{
	IAreaManager areaManager = AreaFactory.getAreaManager();
	
	public PhoneNumberListAdapter(Context context, List<PhoneNumber> list) {
		super(context, list);
	}

	@Override
	protected View getItemView(int position, View convertView, ViewGroup parent) {
		ViewHolder viewHolder = null;
		
		if (convertView == null)
		{
			convertView = inflater.inflate(R.layout.pn_list_item_layout, null);
			
			viewHolder = new ViewHolder();
			viewHolder.telOperatorImageView = (ImageView)convertView.findViewById(R.id.iv_logo_brands);
			viewHolder.phoneNumberTextView = (TextView)convertView.findViewById(R.id.tv_phone_number);
			viewHolder.callPriceTextView = (TextView)convertView.findViewById(R.id.tv_call_price);
			viewHolder.totalPriceTextView = (TextView)convertView.findViewById(R.id.tv_total_price);
			viewHolder.areaTextView = (TextView)convertView.findViewById(R.id.tv_area);
			convertView.setTag(viewHolder);
		}
		else 
		{
			viewHolder = (ViewHolder)convertView.getTag();
		}
		
		PhoneNumber phoneNumber = getItem(position);
		
		if (phoneNumber != null)
		{
			String areaStr = null;
			AreaInfo areaInfo = areaManager.getAreaInfoByAreaId(phoneNumber.getAreaId());
			
			if (areaInfo == null)
			{
				areaStr = "未知";
			}
			else
			{
				areaStr = areaInfo.getAreaName();
				
				if (areaInfo.getParentAreaId() != 0)
				{
					areaInfo = areaManager.getAreaInfoByAreaId(areaInfo.getParentAreaId());
					
					if (areaInfo != null)
					{
						areaStr = areaInfo.getAreaName() + areaStr;
					}
				}
				
			}
			if (phoneNumber.getTelOperatorId() == TelOperatorType.yd.getValue())
			{
				viewHolder.telOperatorImageView.setImageResource(R.drawable.brands_yd);
			}
			else if (phoneNumber.getTelOperatorId() == TelOperatorType.lt.getValue())
			{
				viewHolder.telOperatorImageView.setImageResource(R.drawable.brands_lt);
			}
			else if (phoneNumber.getTelOperatorId() == TelOperatorType.dx.getValue())
			{
				viewHolder.telOperatorImageView.setImageResource(R.drawable.brands_dx);
			}
			
			viewHolder.phoneNumberTextView.setText(getNumberFormat(phoneNumber.getPhoneNumber()));
			viewHolder.callPriceTextView.setText("卡费:" + String.valueOf(phoneNumber.getCallPrice()));
			viewHolder.totalPriceTextView.setText(String.valueOf(phoneNumber.getTotalPrice()));
			viewHolder.areaTextView.setText(areaStr);
		}
		return convertView;
	}
	
	private Spanned getNumberFormat(String phoneNumber)
	{
		Spanned htmlString = null;
		if (phoneNumber == null || phoneNumber.length() < 11)
		{
			return Html.fromHtml(phoneNumber);
		}
		
		htmlString = Html.fromHtml("<font color=\"#EF8F65\">" + phoneNumber.substring(0,3) + "</font> <font color=\"#007CC3\">" + phoneNumber.substring(3,7) + "</font> <font color=\"#DA251D\">" + phoneNumber.substring(7,11) + "</font>");
		return htmlString;
	}
	
	class ViewHolder
	{
		public ImageView telOperatorImageView = null;
		public TextView phoneNumberTextView = null;
		public TextView callPriceTextView = null;
		public TextView totalPriceTextView = null;
		public TextView areaTextView = null;
	}

}
